package com.geek.ch06.anno;

import com.geek.entity.Orders;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 作业1：查询订单信息及其下单者信息（OO - 一对一关系）
 */
public interface UserAndOrderMapper {

    /**
     * 查询所有订单信息及其下单者信息
     * @return 订单列表，每个订单包含下单者信息
     */
    @Select("select * from orders")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "number", column = "number"),
            @Result(property = "createtime", column = "createtime"),
            @Result(property = "note", column = "note"),
            @Result(property = "user", column = "user_id", javaType = com.geek.entity.User.class,
                    one = @One(select = "com.geek.ch06.anno.UserMapper.findUserById"))
    })
    public List<Orders> findAllOrdersWithUser();

    /**
     * 根据订单ID查询订单信息及其下单者信息
     * @param oid 订单ID
     * @return 订单信息，包含下单者信息
     */
    @Select("select * from orders where id = #{oid}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "number", column = "number"),
            @Result(property = "createtime", column = "createtime"),
            @Result(property = "note", column = "note"),
            @Result(property = "user", column = "user_id", javaType = com.geek.entity.User.class,
                    one = @One(select = "com.geek.ch06.anno.UserMapper.findUserById"))
    })
    public Orders findOrderWithUserById(int oid);
}
